﻿// DigitalDesign
// Project: InformationBoard
// Developer: Syzdykov Sergey
// Created: 13.11.2011
// Last change: 19.11.2011
// Description: methods of clients page.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GUIAdmin.AdminServiceReference;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace GUIAdmin
{
    public partial class Clients : Page
    {
        private AdminServiceClient adminClient;
        private ObservableCollection<ClientNotEF> clientList;        
        private int selectedIndex;
        public Clients()
        {
            InitializeComponent();
            adminClient = new AdminServiceClient();
            clientList = new ObservableCollection<ClientNotEF>();
            ClientGrid.ItemsSource = clientList;
            adminClient.GetListOfStationClientsCompleted += FillClientList;
            adminClient.GetListOfPassengersCompleted += FillClassesList;
            adminClient.GetListOfStationLocationsCompleted += FillLocationList;
            adminClient.AddClientCompleted += updateClientList;
            adminClient.UpdateClientCompleted += updateClientList;
            adminClient.DeleteClientCompleted += updateClientList;
        }

        #region buttonclick
        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            if (ClientGrid.SelectedItem != null)
            {
                adminClient.DeleteClientAsync((ClientGrid.SelectedItem as ClientNotEF).Id);
            }

        }

        private void AddClient_Click(object sender, RoutedEventArgs e)
        {
            showAddPanel();
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            if (this.DataContext != null && Ping.Text != "" && VideoInterrupt.Text != "" &&
                    Screenshots.Text != "" && ClassOfPassengersComboBox.SelectedItem != null
                    && LocationComboBox.SelectedItem != null)
            {
                ClientNotEF clientNotEF = new ClientNotEF();
                try
                {
                    clientNotEF.Id = (ClientGrid.SelectedItem as ClientNotEF).Id;
                    clientNotEF.Ticker = ticker.Text;
                    clientNotEF.PingFrequency = Int32.Parse(Ping.Text);
                    if (PossibilityOfUpdateVideo.IsChecked != null)
                        clientNotEF.PossibilityOfUpdateVideo = PossibilityOfUpdateVideo.IsChecked.Value;
                    clientNotEF.ClassOfPassengers =
                        (ClassOfPassengersComboBox.SelectedItem as ClassesOfPassengerNotEF);
                    clientNotEF.ClientLocation = (LocationComboBox.SelectedItem as LocationNotEF);
                    clientNotEF.ScreenshotFrequency = Int32.Parse(Screenshots.Text);
                    clientNotEF.VideoInterruptFrequency = Int32.Parse(VideoInterrupt.Text);
                    adminClient.UpdateClientAsync(clientNotEF);
                    selectedIndex = ClientGrid.SelectedIndex;
                }
                catch
                {

                }
            }

        }

        private void CreateClient_Click(object sender, RoutedEventArgs e)
        {
            if (this.DataContext != null && Ping.Text != "" && VideoInterrupt.Text != "" &&
                Screenshots.Text != "" && ClassOfPassengersComboBox.SelectedItem != null
                && LocationComboBox.SelectedItem != null)
            {
                ClientNotEF clientNotEF = new ClientNotEF();
                try
                {
                    clientNotEF.Ticker = ticker.Text;
                    clientNotEF.PingFrequency = Int32.Parse(Ping.Text);
                    if (PossibilityOfUpdateVideo.IsChecked != null)
                        clientNotEF.PossibilityOfUpdateVideo = PossibilityOfUpdateVideo.IsChecked.Value;
                    clientNotEF.ClassOfPassengers =
                        (ClassOfPassengersComboBox.SelectedItem as ClassesOfPassengerNotEF);
                    clientNotEF.ClientLocation = (LocationComboBox.SelectedItem as LocationNotEF);
                    clientNotEF.ScreenshotFrequency = Int32.Parse(Screenshots.Text);
                    clientNotEF.VideoInterruptFrequency = Int32.Parse(VideoInterrupt.Text);
                    adminClient.AddClientAsync(clientNotEF);
                    selectedIndex = -1;
                }
                catch
                {

                }

            }
        }

        private void ClientGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            showChangePanel();
        }

        #endregion

        #region clientEvent
        private void FillClientList(object sender, GetListOfStationClientsCompletedEventArgs e)
        {
            clientList.Clear();
            foreach (var location in e.Result)
            {
                clientList.Add(location);
            }

            if (clientList.Count != 0 && selectedIndex != -1)
            {
                if (clientList.Count < selectedIndex + 1)
                    ClientGrid.SelectedIndex = clientList.Count - 1;
                else
                    ClientGrid.SelectedIndex = selectedIndex;
            }
            else
            {
                showAddPanel();
            }

        }

        private void updateClientList(object sender, AsyncCompletedEventArgs e)
        {
            if (this.DataContext != null)
                adminClient.GetListOfStationClientsAsync((this.DataContext as StationNotEF).Id);
        }

        private void FillClassesList(object sender, GetListOfPassengersCompletedEventArgs e)
        {
            ClassOfPassengersComboBox.Items.Clear();
            foreach (var passengerClass in e.Result)
            {
                ClassOfPassengersComboBox.Items.Add(passengerClass);
            }
            
            if (ClientGrid.SelectedItem != null)
            {
                for (int i = 0; i < ClassOfPassengersComboBox.Items.Count; i++)
                {
                    if ((ClassOfPassengersComboBox.Items[i] as ClassesOfPassengerNotEF).Id ==
                        (ClientGrid.SelectedItem as ClientNotEF).ClassOfPassengers.Id)
                    {
                        ClassOfPassengersComboBox.SelectedIndex = i;
                        break;
                    }
                }
            }
        }

        private void FillLocationList(object sender, GetListOfStationLocationsCompletedEventArgs e)
        {
            LocationComboBox.Items.Clear();
            foreach (var location in e.Result)
            {
                LocationComboBox.Items.Add(location);
            }

            if (ClientGrid.SelectedItem != null)
            {
                for (int i = 0; i < LocationComboBox.Items.Count; i++)
                {
                    if ((LocationComboBox.Items[i] as LocationNotEF).Id ==
                        (ClientGrid.SelectedItem as ClientNotEF).ClientLocation.Id)
                    {
                        LocationComboBox.SelectedIndex = i;
                        break;
                    }
                }
            }
        }

        private void FillAdditionalLists()
        {
            adminClient.GetListOfPassengersAsync();
            if (this.DataContext != null)
                adminClient.GetListOfStationLocationsAsync((this.DataContext as StationNotEF).Id);
        }
        #endregion

        #region other

        private void showAddPanel()
        {
            Save.Visibility = Visibility.Collapsed;
            CreateClient.Visibility = Visibility.Visible;
            ChangeClient.Visibility = Visibility.Visible;
            ticker.Text = "";
            PossibilityOfUpdateVideo.IsChecked = false;
            Ping.Text = "";
            Screenshots.Text = "";
            VideoInterrupt.Text = "";
            LocationComboBox.SelectedIndex = -1;
            ClassOfPassengersComboBox.SelectedIndex = -1;
        }



        private void showChangePanel()
        {
            Save.Visibility = Visibility.Visible;
            CreateClient.Visibility = Visibility.Collapsed;
            ChangeClient.Visibility = Visibility.Visible;
            if (ClientGrid.SelectedItem != null)
            {
                if ((ClientGrid.SelectedItem as ClientNotEF).Ticker != null)
                    ticker.Text = (ClientGrid.SelectedItem as ClientNotEF).Ticker;
                else
                    ticker.Text = "";
                PossibilityOfUpdateVideo.IsChecked =
                    (ClientGrid.SelectedItem as ClientNotEF).PossibilityOfUpdateVideo;
                Ping.Text =
                    (ClientGrid.SelectedItem as ClientNotEF).PingFrequency.ToString();
                Screenshots.Text =
                    (ClientGrid.SelectedItem as ClientNotEF).ScreenshotFrequency.ToString();
                VideoInterrupt.Text =
                    (ClientGrid.SelectedItem as ClientNotEF).VideoInterruptFrequency.ToString();
                FillAdditionalLists();
                ClassOfPassengersComboBox.SelectedItem =
                    (ClientGrid.SelectedItem as ClientNotEF).ClassOfPassengers;
                LocationComboBox.SelectedItem =
                    (ClientGrid.SelectedItem as ClientNotEF).ClientLocation;
                selectedIndex = ClientGrid.SelectedIndex;
            }
        }
        #endregion

        // Выполняется, когда пользователь переходит на эту страницу.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            ChangeClient.Visibility = Visibility.Collapsed;

            if (DataContext != null)
                adminClient.GetListOfStationClientsAsync((this.DataContext as StationNotEF).Id);

            FillAdditionalLists();
        }


    }
}